package com.yixq.soa.service.web.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PasswordUtils {
	private  static final Logger logger = LoggerFactory.getLogger(PasswordUtils.class);
	
	private static String PASSWORD_SUFF = "BanShengHuO_pWd";
	public static String encodePwdByOrgin(String org){
		String pwd = null;
		if(org == null){
			throw new IllegalArgumentException("password can't be null");
		}
		
		pwd = encodePwdByMD5( md5(org) );
		
		return pwd;
	}
	
	public static String encodePwdByMD5(String md5){
		if(md5 == null){
			throw new IllegalArgumentException("password can't be null");
		}
		
		String pwd = md5( md5 + PASSWORD_SUFF );
		return pwd;
	}
	
	public static String md5(String str){
		String md5str = null;
		try {
			MessageDigest md5d=MessageDigest.getInstance("MD5");
			md5str = BytesConvertToHexString(md5d.digest(str.getBytes()));
		} catch (NoSuchAlgorithmException e) {
			logger.error("",e);
		}
		
		return md5str;
	}
	
	private static String BytesConvertToHexString(byte [] bytes) {
	       StringBuffer sb = new StringBuffer();
	       for (byte aByte : bytes) {
	         String s=Integer.toHexString(0xff & aByte);
	           if(s.length()==1){
	               sb.append("0"+s);
	           }else{
	               sb.append(s);
	           }
	       }
	       return sb.toString();
	   }
}
